package com.gimi.cloud.bbp.manage.consumer.service.prod.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.StringUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.dto.prod.EcProdBrandDTO;
import com.gimi.cloud.bbp.manage.consumer.feign.EcProdBrandFeignService;
import com.gimi.cloud.bbp.manage.consumer.service.prod.BrandService;

@Service
public class BrandServiceImpl implements BrandService {
    private static final Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
    private final EcProdBrandFeignService prodBrandFeignService;

    @Autowired
    public BrandServiceImpl(EcProdBrandFeignService prodBrandFeignService){
        this.prodBrandFeignService = prodBrandFeignService;
    }

    @Override
    public Response getBrandList(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) {
        EcProdBrandDTO ecProdBrandDTO = JsonUtil.jsonToBean(requestBody,EcProdBrandDTO.class);
        return prodBrandFeignService.getBrandList(ecProdBrandDTO);
    }

    @Override
    public Response addBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdBrandDTO ecProdBrandDTO = JsonUtil.jsonToBean(requestBody,EcProdBrandDTO.class);
        if(null != ecProdBrandDTO.getPdId()){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"主键不为空，添加品牌失败");
        }
        if(StringUtil.isBlank(ecProdBrandDTO.getProdBrandDesk())||StringUtil.isBlank(ecProdBrandDTO.getProdBrandLogo())||
                StringUtil.isBlank(ecProdBrandDTO.getProdBrandName())|| StringUtil.isBlank(ecProdBrandDTO.getType())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.saveOrUpdate(ecProdBrandDTO);
    }

    @Override
    public Response updateBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdBrandDTO ecProdBrandDTO = JsonUtil.jsonToBean(requestBody,EcProdBrandDTO.class);
        if(null == ecProdBrandDTO.getPdId()){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"主键为空，更新品牌失败");
        }
        if(StringUtil.isBlank(ecProdBrandDTO.getPdId())||StringUtil.isBlank(ecProdBrandDTO.getProdBrandDesk())||StringUtil.isBlank(ecProdBrandDTO.getProdBrandLogo())||
                StringUtil.isBlank(ecProdBrandDTO.getProdBrandName())|| StringUtil.isBlank(ecProdBrandDTO.getType())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.saveOrUpdate(ecProdBrandDTO);
    }

    @Override
    public Response deleteBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdBrandDTO ecProdBrandDTO = JsonUtil.jsonToBean(requestBody,EcProdBrandDTO.class);
        Response resp = new Response();
        if(null!= ecProdBrandDTO && !StringUtil.isBlank(ecProdBrandDTO.getPdId()) && null != ecProdBrandDTO.getType()){
            resp = prodBrandFeignService.brandDelete(ecProdBrandDTO);
        }else{
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
        }
		return resp;
    }

    @Override
    public Response getBrandPageList(String requestBody, HttpServletRequest request, HttpServletResponse response)  throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdBrandDTO ecProdBrandDTO = JsonUtil.jsonToBean(requestBody,EcProdBrandDTO.class);
//        if(StringUtil.isBlank(ecProdBrandDTO.getPageSize())||StringUtil.isBlank(ecProdBrandDTO.getCurrentPage())){
//            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
//        }
        return prodBrandFeignService.getBrandPageList(ecProdBrandDTO);
    }

    @Override
    public Response gotoEditProdBrand(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        EcProdBrandDTO ecProdBrandDTO = JsonUtil.jsonToBean(requestBody,EcProdBrandDTO.class);
        if(StringUtil.isBlank(ecProdBrandDTO.getPdId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodBrandFeignService.gotoEditProdBrand(ecProdBrandDTO);
    }

    @Override
    public Response fileUpload(HttpServletResponse response, HttpServletRequest request)  throws Exception{
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap<String, MultipartFile> multiFileMap = multipartHttpServletRequest.getMultiFileMap();
        return prodBrandFeignService.fileUpload(multiFileMap);

    }
}
